package rpg.action;

import rpg.Rpg;
import rpg.map.Creature;
import rpg.map.MapObject;
import rpg.script.SkillScriptImpl;
import square.data.DataGroup;
import square.data.SkillData;

public class Skill extends Action {
	private SkillData data, dataReflect;
	private SkillScriptImpl script;
	private Long lastUse = 0L;

	// Creation
	public Skill(Creature creature, SkillData data, SkillData reflectData) {
		this.setCreature(creature);
		this.setData(data);
		this.setDataReflect(reflectData);

		if (this.getData().getScript() != null) {
			this.setScript((SkillScriptImpl) this.getData().getScript()
					.instance());
			this.getScript().setSkill(this);
		}
	}

	public Skill(Creature creature, SkillData data) {
		this(creature, data, data.instance());
	}

	public Skill(Creature creature, String name) {
		this(creature, DataGroup.findSkill(name));
	}

	// Use Event
	public boolean onUse(MapObject mapObject) {
		if (Rpg.disableEvents)
			return false;

		if (this.getScript() != null) {
			if (getScript().onUse(mapObject)) {
				this.setLastUse(System.currentTimeMillis());
				return true;
			}
			return false;
		}
		return true;
	}

	// Available Control
	public boolean available() {
		if (System.currentTimeMillis() - this.getLastUse() > this.getData()
				.getDelay())
			return true;
		return false;
	}

	// Get & Set
	public Long getLastUse() {
		return lastUse;
	}

	public void setLastUse(Long lastUse) {
		this.lastUse = lastUse;
	}

	public SkillData getData() {
		return data;
	}

	public void setData(SkillData data) {
		this.data = data;
	}

	public SkillData getDataReflect() {
		return dataReflect;
	}

	public void setDataReflect(SkillData dataReflect) {
		this.dataReflect = dataReflect;
	}

	public SkillScriptImpl getScript() {
		return script;
	}

	public void setScript(SkillScriptImpl script) {
		this.script = script;
	}
}
